﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;

namespace Shop
{
    [Serializable()]
    class Shop
    {
        List<Tovar> products = new List<Tovar>();
        public Shop(string name)
        {
            _name = name;
        }
        protected string _name;
        protected double _cost;
        public string name
        {
            get { return _name; }
            set { _name = value; }
        }
        public double cost
        {
            get { return _cost; }
            set { _cost = value; }
        }
        int Id() //вычисление последнего ID в списке товаров 
        {
            int tempId = 0;
            if (products.Count > 0)
            {
                tempId = products[0].id_tovar;
                for (int i = 1; i < products.Count; i++)
                {
                    if (tempId < products[i].id_tovar)
                        tempId = products[i].id_tovar;
                }
                tempId++;
            }
            else
                tempId = 1;
            return tempId;
        }
        public void AddTovar(double weight, string name, double cost, DateTime dateOfCreate, DateTime period, 
            string placeOfCreate, string description, double procent)//добавление товара по весу
        { 
            TovarOfWeight temp = new TovarOfWeight(weight,name,cost,dateOfCreate,
                                                    period,placeOfCreate,description,procent,Id());
            products.Add(temp);
        }

        public void AddTovar(int count, string name, double cost, DateTime dateOfCreate, DateTime period,
            string placeOfCreate, string description, double procent)//добавление товара по количеству
        {
            TovarOfCount temp = new TovarOfCount(count, name, cost, dateOfCreate,
                                                    period, placeOfCreate, description, procent, Id());
            products.Add(temp);
        }

        public List<String> ListOfTovar()
        { 
            List<String> temp = new List<string>();
            for (int i = 0; i < products.Count; i++)
            {
                temp.Add(products[i].id_tovar + " Name: " +products[i].name+" Cost: "+products[i].costOfRealization+ " Period of: "+products[i].period);
            }
                return temp;
        } //Возвращает список товаров в вите строки для вывода
        public List<int> NumberTovar()
        {
            List<int> temp = new List<int>();
            for (int i = 0; i < products.Count; i++)
            {
                temp.Add(products[i].id_tovar);
            }
            return temp;
        }

        public void DeleteTovar(int id) // удаление товаа по индексу
        {
            for (int i = 0; i < products.Count; i++)
            {
                if (id == products[i].id_tovar)
                {
                    products.RemoveAt(i);
                    break;
                }
            }
        }

        public Tovar getTovar(int id)
        {
            Tovar temp = new Tovar();
            for (int i = 0; i < products.Count; i++)
            {
                if (id == products[i].id_tovar)
                {
                    temp = products[i];
                    break;
                }
            }
            return temp;
        }

        public void setTovar(Tovar tovar)
        {
            for (int i = 0; i < products.Count; i++)
            {
                if (tovar.id_tovar == products[i].id_tovar)
                {
                    products[i] = tovar;
                    break;
                }
            }
        }

        public void decCount(int idTovar,int count)
        {
            for (int i = 0; i < products.Count; i++)
            {
                if (idTovar == products[i].id_tovar)
                {
                    if (products[i] is TovarOfCount)
                    {
                        ((TovarOfCount)products[i]).count -= count;
                    } else
                        ((TovarOfWeight)products[i]).weight -= count;
                    break;
                }
            }
        }
    }
}
